package com.dudu.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dudu.access.ImsUploadFile;
import com.dudu.access.RongLianUtils;
import com.dudu.access.TianYiUtil;
import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.bean.ChargingRuleBean;
import com.dudu.bean.PoolAbilityInfoBean;
import com.dudu.bean.ResponseSendVoiceBean;
import com.dudu.dao.SendNoticeDao;
import com.dudu.dao.UploadFileDao;
import com.dudu.enumeration.AbilityTypeEnum;
import com.dudu.model.SendNotice;
import com.dudu.model.UploadFileModel;
import com.dudu.service.IAudeoNoticeService;
import com.dudu.service.IRuleService;
import com.dudu.timer.SendVoiceUtil;
import com.dudu.timer.UploadFileThread;
import com.dudu.utils.Encrypt;
import com.dudu.utils.EnumUtility;
import com.dudu.utils.ProperyResource;
import com.dudu.utils.SsoUtil;
import com.dudu.utils.VoiceUtil;

import core.CoreDao;
import core.CoreDaoInvoke;

/**
 * 发去语音留言逻辑类
 * 
 * @author 危东
 * @date 2016-5-6
 */
public class AudeoNoticeServiceImpl implements IAudeoNoticeService {
    // 用于查询数据用
    private CoreDao coreDao = CoreDaoInvoke.getInstance();

    private static final Logger log = core.util.Logger.getLogger(AudeoNoticeServiceImpl.class.getSimpleName());
    // 语音信息
    private SendNoticeDao mSendNoticeDao = new SendNoticeDao();
    private UploadFileDao uploadfileDao = new UploadFileDao();

    /**
     * 上传语音文件
     * 
     * @param app_key
     * @param cust_account
     * @param fileAddress
     * @param mediaName
     * @return
     */
    public JSONObject uploadFile(String app_key, String cust_account, String fileAddress, String mediaName, String content,String fileFullName) {
	JSONObject object = new JSONObject();
	SsoUtil sso = new SsoUtil();
	if(StringUtils.isBlank(fileFullName)){
	    fileFullName=SsoUtil.getNewMediaName(mediaName);
	    sso.putNewFile(fileFullName, fileAddress);// 上传到SSO
	}
	int i = uploadfileDao.insertFileInfo(coreDao, app_key, cust_account, fileAddress, mediaName, "", content,fileFullName);
	if (i != -1) {
	    String downUrl=sso.getOssAbsPath(fileFullName);
	    if (StringUtils.isNotBlank(fileFullName)) {
		object.put("result", "0");
		object.put("describe", "文件上传成功");
		object.put("code", "0000");
		object.put("mediaName", mediaName);
		object.put("voice_file", fileFullName);
		object.put("downUrl", downUrl);
	    } else {
		object.put("result", "-1");
		object.put("describe", "文件上传失败");
		object.put("code", "0503");
		object.put("mediaName", "");
		object.put("voice_file", "");
	    }
	} else {
	    object.put("result", "-1");
	    object.put("describe", "文件上传失败");
	    object.put("code", "0503");
	    object.put("mediaName", "");
	}
	return object;
    }

    /**
     * 发起语音留言(新版)
     * 
     * @param appExtendBean
     *            应用信息
     * @param accountBean
     *            账号信息
     * @param userIdentify
     *            用户标识
     * @param caller
     *            主叫号码
     * @param called
     *            被叫号码
     * @param mediaName
     *            语音文件名称
     * @param schedule_send_time
     *            预定短信发起时间
     * @param push_url
     *            sessionid推送地址
     * @param tts_content
     *            前导音
     * @param pool_ability_id
     *            ISV自定义发起留言使用号码
     * @param batch_number
     *            是否一批次
     * @param usePlatNbr
     *            强制使用平台号
     * @return
     */
    public JSONObject sendAudeoNoticeV4(AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify, String caller, String called, String mediaName,
	    String schedule_send_time, String push_url, String tts_content, String pool_ability_id, String batch_number, String usePlatNbr, JSONArray calledJsonArray,
	    String voice_type) {
	log.info("AudeoNoticeServiceImpl.sendAudeoNoticeV4 发起语音留言");
	// 处理前导音的标点
	tts_content = tts_content.replaceAll("\\pP|\\pS", " ");
	// 最终返回值
	JSONObject object = new JSONObject();
	// 获取语音短信能力类型标识
	String abilityType = AbilityTypeEnum.AUDEO.getKey();
	abilityType = VoiceUtil.changeAbilityTypeByVoiceType(abilityType, voice_type, "audio");
	// 能力配置查询
	IRuleService ruleService = new RuleServiceImpl();
	ChargingRuleBean chargingRule = ruleService.queryChargingRule(appExtendBean.getApp_key(), accountBean.getCust_account(), abilityType);

	if (chargingRule != null) {
	    // 判断如果传入的时间为空，则默认使用当前时间+3分钟
	    if (chargingRule.getType().contains("YonYou")) { // 自建通道设置为空发，SendVoiceUtil.java类统一处理发送时间
		if (schedule_send_time == null || schedule_send_time.equals("") || schedule_send_time.equals("null")) {
		    schedule_send_time = "";
		}
	    } else {
		if (schedule_send_time == null || schedule_send_time.equals("") || schedule_send_time.equals("null")) {
		    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    Date now = new Date();
		    Date nowAdd = new Date(now.getTime() + 3 * 60 * 1000);
		    schedule_send_time = format.format(nowAdd);
		}
	    }

	    String displayNum = null;// 显号
	    String number_vest = null;// 地区
	    SendVoiceUtil sendUtil = new SendVoiceUtil();
	    PoolAbilityInfoBean poolAbilityInfo = sendUtil.queryPoolAbilityInfo(coreDao, appExtendBean.getApp_key(), accountBean.getCust_account(), chargingRule.getType(),
		    pool_ability_id, displayNum, usePlatNbr, number_vest);
	    if (poolAbilityInfo == null) {
		if (chargingRule.getType().startsWith("RongLian")) {
		    poolAbilityInfo = new PoolAbilityInfoBean();
		} else {
		    object.put("result", "-1");
		    object.put("errorMsg", "未配置对应显号;ability_type:" + chargingRule.getType());
		    object.put("describe", "未配置显号");
		    object.put("code", "0018");
		    return object;
		}
	    }

	    // 判断如果是ims语音信息，则调用ims语音信息接口
	    if (AbilityTypeEnum.AUDEOSEND.getKey().equals(chargingRule.getType())) {
		// ims录音语音通知
		object = sendNotice(poolAbilityInfo, appExtendBean, accountBean, userIdentify, caller, called, mediaName, schedule_send_time, push_url, tts_content,
			pool_ability_id, batch_number, chargingRule.getType(), calledJsonArray);
	    } else if (chargingRule.getType().startsWith("RongLian")) {
		// 容联录音语音通知
		object = sendNotice(poolAbilityInfo, appExtendBean, accountBean, userIdentify, caller, called, mediaName, schedule_send_time, push_url, tts_content,
			pool_ability_id, batch_number, chargingRule.getType(), calledJsonArray);
	    } else if (chargingRule.getType().startsWith("TianYi")) {
		// 天亿录音语音通知
		object = sendNotice(poolAbilityInfo, appExtendBean, accountBean, userIdentify, caller, called, mediaName, schedule_send_time, push_url, tts_content,
			pool_ability_id, batch_number, chargingRule.getType(), calledJsonArray);
	    } else if (chargingRule.getType().startsWith("YonYou")) {
		// 用友自建平台语音通知
		object = sendNotice(poolAbilityInfo, appExtendBean, accountBean, userIdentify, caller, called, mediaName, schedule_send_time, push_url, tts_content,
			pool_ability_id, batch_number, chargingRule.getType(), calledJsonArray);
	    } else {
		object.put("result", "-1");
		object.put("errorMsg", "未配置默认文字语音信息通道！voice_type:" + chargingRule.getType());
		object.put("describe", "未配置线路");
		object.put("code", "0010");
	    }
	} else {
	    object.put("result", "-1");
	    object.put("describe", "能力配置错误");
	    object.put("code", "0013");
	}

	return object;
    }

    /**
     * 语音留音(新版)
     * 
     * @param poolAbilityInfo
     * @param appExtendBean
     * @param accountBean
     * @param userIdentify
     * @param caller
     * @param called
     * @param mediaName
     * @param schedule_send_time
     * @param push_url
     * @param tts_content
     * @param pool_ability_id
     * @param batch_number
     * @param voice_type
     * @return
     */
    private JSONObject sendNotice(PoolAbilityInfoBean poolAbilityInfo, AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify, String caller, String called,
	    String mediaName, String schedule_send_time, String push_url, String tts_content, String pool_ability_id, String batch_number, String voice_type,
	    JSONArray calledJsonArray) {
	// 最终返回结果
	JSONObject object = new JSONObject();
	UploadFileModel fileModel = uploadfileDao.queryFileInfoBymediaName(coreDao, mediaName);
	if (null == fileModel) {
	    object.put("result", "-1");
	    object.put("describe", "语音文件名称未找到,请重新上传");
	    object.put("code", "0510");
	} else {
	    // 向通道上传文件
	    JSONObject obj = SendVoiceUtil.uploadFile(coreDao, fileModel, mediaName, voice_type);
	    if (obj.optString("result").equals("-1")) {
		return obj;
	    }
	    // 存储转发
	    JSONObject voiceAndSessionIds = SendVoiceUtil.saveNotices(called, voice_type, appExtendBean.getApp_key(), caller, poolAbilityInfo.getCharge_number(),
		    poolAbilityInfo.getCharge_key(), accountBean.getCust_account(), appExtendBean.getPeriphery_code(), userIdentify, schedule_send_time, "", "",
		    fileModel.getVoice_name(), push_url, tts_content, poolAbilityInfo.getDisplay_number(), batch_number, "-1", fileModel.getFileAddress(), mediaName, coreDao,
		    calledJsonArray);

	    /**
	     * qinfx 2017-05-22 新增返回session_id
	     */
	    JSONObject voice_ids = voiceAndSessionIds.getJSONObject("voiceIds");
	    JSONObject session_ids = voiceAndSessionIds.getJSONObject("sessionIds");

	    // 拼入返回的会议id
	    object.put("voice_id", voice_ids.toString());
	    object.put("session_id", session_ids.toString());
	    object.put("result", "0");
	    object.put("describe", "语音留言存储成功");
	    object.put("code", "0000");
	}
	return object;
    }

    /**
     * 重新发起语音短信
     * 
     * @param appExtendBean
     *            应用信息
     * @param accountBean
     *            账号信息
     * @param userIdentify
     *            用户标识
     * @param voice_id
     *            重发的信息的id
     * @param caller
     *            主叫号码
     * @param called
     *            被叫号码
     * @param schedule_send_time
     *            预定短信发起时间
     * @param push_url
     *            语音通信预约后，通信发起sessionid接收地址
     * @param tts_content
     *            前导音 如果设置前导音，则使用之前的前导音如果前导音设置为-1，则清空前导音
     * @param pool_ability_id
     *            ISV自定义发起留言使用号码
     * @param batch_number
     *            是否一批次
     * @param usePlatNbr
     *            强制使用平台号
     * @return
     */
    @Override
    public JSONObject sendAudeoNoticeLet(AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify, String voice_id, String caller, String called,
	    String schedule_send_time, String push_url, String tts_content, String pool_ability_id, String batch_number, String usePlatNbr, JSONArray calledJsonArray,
	    String voice_type) {
	// 最终返回结果
	JSONObject object = new JSONObject();

	// 判断如果传入的时间为空，则默认使用当前时间
	if (schedule_send_time == null || schedule_send_time.equals("") || schedule_send_time.equals("null")) {
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Date now = new Date();
	    schedule_send_time = format.format(now);
	}
	// 查询当前账号默认使用的多方通话能力通道
	// 获取语音短信能力类型标识
	String abilityType = AbilityTypeEnum.AUDEO.getKey();
	abilityType = VoiceUtil.changeAbilityTypeByVoiceType(abilityType, voice_type, "audio");
	// 能力配置查询
	IRuleService ruleService = new RuleServiceImpl();
	ChargingRuleBean chargingRule = ruleService.queryChargingRule(appExtendBean.getApp_key(), accountBean.getCust_account(), abilityType);
	// 判断查询的默认通道调用数据是否为空
	if (chargingRule != null) {
	    String displayNum = null;// 显号
	    String number_vest = null;// 地区
	    SendVoiceUtil sendUtil = new SendVoiceUtil();
	    PoolAbilityInfoBean poolAbilityInfo = sendUtil.queryPoolAbilityInfo(coreDao, appExtendBean.getApp_key(), accountBean.getCust_account(), chargingRule.getType(),
		    pool_ability_id, displayNum, usePlatNbr, number_vest);
	    if (poolAbilityInfo == null) {
		if (chargingRule.getType().startsWith("RongLian")) {
		    poolAbilityInfo = new PoolAbilityInfoBean();
		} else {
		    object.put("result", "-1");
		    object.put("errorMsg", "未配置对应显号;ability_type:" + chargingRule.getType());
		    object.put("describe", "未配置显号");
		    object.put("code", "0018");
		    return object;
		}
	    }

	    // 判断如果是ims文字语音信息，则调用ims文字语音信息接口
	    if (AbilityTypeEnum.AUDEOSEND.getKey().equals(chargingRule.getType())) {
		// ims语音通知
		object = sendNoticeLet(poolAbilityInfo, voice_id, appExtendBean, accountBean, userIdentify, caller, called, schedule_send_time, push_url, chargingRule.getType(),
			tts_content, pool_ability_id, batch_number, calledJsonArray);
	    } else if (chargingRule.getType().startsWith("RongLian")) {
		// 荣联语音通知
		object = sendNoticeLet(poolAbilityInfo, voice_id, appExtendBean, accountBean, userIdentify, caller, called, schedule_send_time, push_url, chargingRule.getType(),
			tts_content, pool_ability_id, batch_number, calledJsonArray);
	    } else if (chargingRule.getType().startsWith("TianYi")) {
		// 天亿语音通知
		object = sendNoticeLet(poolAbilityInfo, voice_id, appExtendBean, accountBean, userIdentify, caller, called, schedule_send_time, push_url, chargingRule.getType(),
			tts_content, pool_ability_id, batch_number, calledJsonArray);
	    } else if (chargingRule.getType().startsWith("YonYou")) {
		// 用友自建平台语音通知
		object = sendNoticeLet(poolAbilityInfo, voice_id, appExtendBean, accountBean, userIdentify, caller, called, schedule_send_time, push_url, chargingRule.getType(),
			tts_content, pool_ability_id, batch_number, calledJsonArray);
	    } else {
		object.put("result", "-1");
		object.put("describe", "未配置线路");
		object.put("errorMsg", "未配置默认IVR外呼通道！voice_type:" + chargingRule.getType());
		object.put("code", "0010");
	    }
	} else {
	    object.put("result", "-1");
	    object.put("describe", "能力配置错误");
	    object.put("code", "0013");
	}
	return object;
    }

    /**
     * 重发语音短信实际业务处理
     * 
     * @param voice_id
     * @param appExtendBean
     * @param accountBean
     * @param userIdentify
     * @param caller
     * @param called
     * @param schedule_send_time
     * @param push_url
     * @param voice_type
     * @param tts_content
     *            前导音 如果设置前导音为-1，则使用之前的前导音如果前导音设置为空，则清空前导音
     * @param pool_ability_id
     *            ISV自定义发起留言使用号码
     * @param batch_number
     *            是否一批次
     * @return
     */
    private JSONObject sendNoticeLet(PoolAbilityInfoBean poolAbilityInfo, String voice_id, AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify,
	    String caller, String called, String schedule_send_time, String push_url, String voice_type, String tts_content, String pool_ability_id, String batch_number,
	    JSONArray calledJsonArray) {

	// 最终返回结果
	JSONObject object = new JSONObject();
	SendNotice sendNoticeOld = null;
	// 查询需要重发的信息
	sendNoticeOld = mSendNoticeDao.querySendNoticeByMeetId(coreDao, voice_id);
	if (sendNoticeOld == null) {
	    object.put("result", "-1");
	    object.put("describe", "未找到相应的信息" + voice_id);
	    object.put("code", "0512");
	} else {
	    if (!sendNoticeOld.getVoice_type().equals(voice_type)) {
		UploadFileModel fileModel = uploadfileDao.queryFileInfoBymediaName(coreDao, sendNoticeOld.getMediaName());
		if (null == fileModel) {
		    object.put("result", "-1");
		    object.put("describe", "语音文件名称未找到,请重新上传");
		    object.put("code", "0510");
		    return object;
		} else {
		    // 上传
		    UploadFileThread uploadFileThread = new UploadFileThread(coreDao, fileModel, voice_type, mSendNoticeDao, uploadfileDao);
		    uploadFileThread.start();
		    // executorService.execute(uploadFileThread);
		}
		// 由于重新上传文件，则需要3分钟后再发起通知
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		Date nowAdd = new Date(now.getTime() + 3 * 60 * 1000);
		schedule_send_time = format.format(nowAdd);
	    } else {
		if (isNullForVoiceName(voice_type, sendNoticeOld.getVoice_name(), sendNoticeOld.getMediaName())) {
		    object.put("result", "-1");
		    object.put("describe", "文件上传失败或未开始上传,MediaName=" + sendNoticeOld.getMediaName());
		    object.put("code", "0511");
		    return object;
		}
	    }

	    if (tts_content.equals("-1")) {
		tts_content = sendNoticeOld.getTts_content();
	    }
	    String content = sendNoticeOld.getContent();
	    if (voice_type.startsWith(AbilityTypeEnum.YONYOUAUDEO.getKey()) || voice_type.startsWith(AbilityTypeEnum.RONGLIANAUDEO.getKey())
		    || voice_type.startsWith(AbilityTypeEnum.TIANYIAUDEO.getKey())) {
		content = "";
	    }

	    JSONObject voiceAndSessionIds = SendVoiceUtil.saveNotices(called, voice_type, appExtendBean.getApp_key(), caller, poolAbilityInfo.getCharge_number(),
		    poolAbilityInfo.getCharge_key(), accountBean.getCust_account(), appExtendBean.getPeriphery_code(), userIdentify, schedule_send_time, "", content,
		    sendNoticeOld.getVoice_name(), push_url, tts_content, poolAbilityInfo.getDisplay_number(), batch_number, "-1", sendNoticeOld.getCompleteFileName(),
		    sendNoticeOld.getMediaName(), coreDao, calledJsonArray);

	    /**
	     * qinfx 2017-05-22 新增返回session_id
	     */
	    JSONObject voice_ids = voiceAndSessionIds.getJSONObject("voiceIds");
	    JSONObject session_ids = voiceAndSessionIds.getJSONObject("sessionIds");

	    // 拼入返回的会议id
	    object.put("voice_id", voice_ids.toString());
	    object.put("session_id", session_ids.toString());
	    object.put("result", "0");
	    object.put("describe", "重发语音短信存储成功");
	    object.put("code", "0000");
	}

	return object;
    }

    /**
     * 判断 使用语音文件发送通知或者外呼时需要先判断语音文件是否上传成功
     * 
     * @param voice_type
     * @param voice_name
     * @return
     */
    private boolean isNullForVoiceName(String voice_type, String voice_name, String mediaName) {
	// ivr与录音语音通知
	if (voice_type.indexOf("Audeo") > -1 || voice_type.startsWith("Ivr")) {
	    // 使用语音文件发送通知或者外呼时需要先判断语音文件是否上传成功
	    if (StringUtils.isBlank(voice_name) && StringUtils.isNotBlank(mediaName)) {
		return true;
	    } else {
		return false;
	    }
	} else {
	    return false;
	}
    }

    /**
     * 发起语音留言(旧版)
     * 
     * @param appExtendBean
     *            应用信息
     * @param accountBean
     *            账号信息
     * @param userIdentify
     *            用户标识
     * @param caller
     *            主叫号码
     * @param called
     *            被叫号码
     * @param content
     *            短信内容
     * @param schedule_send_time
     *            预定短信发起时间
     * @param paramMap
     * @param completeFileName
     *            文件物理地址
     * @param push_url
     *            sessionid推送地址
     * @param tts_content
     *            前导音
     * @param pool_ability_id
     *            ISV自定义发起留言使用号码
     * @param batch_number
     *            是否一批次
     * @param usePlatNbr
     *            强制使用平台号
     * @deprecated 不在维护
     * @return
     */
    @Override
    public JSONObject sendAudeoNotice(AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify, String caller, String called, String content,
	    String schedule_send_time, Map<String, String> paramMap, String completeFileName, String push_url, String tts_content, String pool_ability_id, String batch_number,
	    String usePlatNbr, JSONArray calledJsonArray, String voice_type) {
	log.info("AudeoNoticeServiceImpl.sendAudeoNotice 发起语音留言");
	// 处理前导音的标点
	tts_content = tts_content.replaceAll("\\pP|\\pS", " ");
	// 最终返回值
	JSONObject object = new JSONObject();
	if (StringUtils.isBlank(caller) || StringUtils.isBlank(called)) {
	    object.put("sessionId", null);
	    object.put("result", "-1");
	    object.put("describe", "主叫号码、被叫号码不能为空");
	    object.put("code", "0020");

	} else {
	    // 判断如果传入的时间为空，则默认使用当前时间+3分钟
	    // if (schedule_send_time == null || schedule_send_time.equals("")
	    // || schedule_send_time.equals("null")) {
	    // SimpleDateFormat format = new
	    // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    // Date now = new Date();
	    // Date nowAdd = new Date(now.getTime() + 3 * 60 * 1000);
	    // schedule_send_time = format.format(nowAdd);
	    // }
	    // 获取语音短信能力类型标识
	    String abilityType = AbilityTypeEnum.AUDEO.getKey();
	    abilityType = VoiceUtil.changeAbilityTypeByVoiceType(abilityType, voice_type, "audio");
	    // 能力配置查询
	    IRuleService ruleService = new RuleServiceImpl();
	    ChargingRuleBean chargingRule = ruleService.queryChargingRule(appExtendBean.getApp_key(), accountBean.getCust_account(), abilityType);
	    if (chargingRule != null) {
		// 判断如果传入的时间为空，则默认使用当前时间+3分钟
		if (chargingRule.getType().contains("YonYou")) { // 自建通道设置为空发，SendVoiceUtil.java类统一处理发送时间
		    if (schedule_send_time == null || schedule_send_time.equals("") || schedule_send_time.equals("null")) {
			schedule_send_time = "";
		    }
		} else {
		    if (schedule_send_time == null || schedule_send_time.equals("") || schedule_send_time.equals("null")) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date now = new Date();
			Date nowAdd = new Date(now.getTime() + 3 * 60 * 1000);
			schedule_send_time = format.format(nowAdd);
		    }
		}

		String displayNum = null;// 显号
		String number_vest = null;// 地区
		SendVoiceUtil sendUtil = new SendVoiceUtil();
		PoolAbilityInfoBean poolAbilityInfo = sendUtil.queryPoolAbilityInfo(coreDao, appExtendBean.getApp_key(), accountBean.getCust_account(), chargingRule.getType(),
			pool_ability_id, displayNum, usePlatNbr, number_vest);

		if (poolAbilityInfo == null) {
		    if (chargingRule.getType().startsWith("RongLian")) {
			poolAbilityInfo = new PoolAbilityInfoBean();
		    } else {
			object.put("result", "-1");
			object.put("describe", "未配置对应显号;ability_type:" + chargingRule.getType());
			object.put("code", "0018");
			return object;
		    }
		}

		// 判断如果是ims语音信息，则调用ims语音信息接口
		if (AbilityTypeEnum.AUDEOSEND.getKey().equals(chargingRule.getType())) {
		    // ims语音通知
		    object = sendAudeoNoticeIms(poolAbilityInfo.getCharge_number(), poolAbilityInfo.getCharge_key(), poolAbilityInfo.getDisplay_number(), appExtendBean,
			    accountBean, userIdentify, caller, called, content, schedule_send_time, paramMap, completeFileName, push_url, tts_content, pool_ability_id,
			    batch_number, calledJsonArray);
		} else if (chargingRule.getType().startsWith("RongLian")) {
		    // 容联语音通知
		    object = sendAudeoNoticeRongLian(poolAbilityInfo.getCharge_number(), poolAbilityInfo.getCharge_key(), poolAbilityInfo.getDisplay_number(), appExtendBean,
			    accountBean, userIdentify, caller, called, content, schedule_send_time, paramMap, completeFileName, push_url, tts_content, pool_ability_id,
			    batch_number, calledJsonArray, chargingRule.getType());
		} else if (chargingRule.getType().startsWith("TianYi")) {
		    // 天亿语音通知
		    object = sendAudeoNoticeTianYi(poolAbilityInfo.getCharge_number(), poolAbilityInfo.getCharge_key(), poolAbilityInfo.getDisplay_number(), appExtendBean,
			    accountBean, userIdentify, caller, called, content, schedule_send_time, paramMap, completeFileName, push_url, tts_content, pool_ability_id,
			    batch_number, calledJsonArray, chargingRule.getType());
		} else {
		    object.put("result", "-1");
		    object.put("describe", "未配置对应录音语音通知通道！voice_type:" + chargingRule.getType());
		    object.put("code", "0010");
		}
	    } else {
		object.put("result", "-1");
		object.put("describe", "未配置默认录音语音通知通道！");
		object.put("code", "0010");
	    }

	}
	return object;
    }

    /**
     * IMS语音留言能力后台逻辑处理（旧版）
     * 
     * @param chargeNbr
     *            IMS发起账号
     * @param key
     *            IMS发起密码
     * @param displayNumber
     *            显号
     * @param appExtendBean
     *            应用信息
     * @param accountBean
     *            账号信息
     * @param userIdentify
     *            用户标识
     * @param caller
     *            主叫号码
     * @param called
     *            被叫号码
     * @param content
     *            短信内容
     * @param schedule_send_time
     *            预定短信发起时间
     * @param paramMap
     * @param completeFileName
     *            文件物理地址
     * @param push_url
     *            sessionid推送地址
     * @param tts_content
     *            前导音
     * @param pool_ability_id
     *            ISV自定义发起留言使用号码
     * @param batch_number
     *            是否一批次
     * @deprecated 不在使用
     * @return
     */
    private JSONObject sendAudeoNoticeIms(String chargeNbr, String key, String displayNumber, AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify,
	    String caller, String called, String content, String schedule_send_time, Map<String, String> paramMap, String completeFileName, String push_url, String tts_content,
	    String pool_ability_id, String batch_number, JSONArray calledJsonArray) {
	// 最终返回值
	JSONObject object = new JSONObject();
	// 语音留言存储状态
	ResponseSendVoiceBean responseSendVoice = null;
	try {

	    paramMap.put("Spid", ProperyResource.getSpId());
	    paramMap.put("Appid", ProperyResource.getAppId());
	    paramMap.put("Passwd", Encrypt.sha1Encrypt(ProperyResource.getPassword()));
	    responseSendVoice = ImsUploadFile.post(ProperyResource.getServiceUploadVoiceUrl(), paramMap, completeFileName);
	} catch (IOException e) {
	    object.put("result", "-1");
	    object.put("describe", "创建语音留言失败" + e);
	    object.put("code", "9999");
	    e.printStackTrace();
	}

	if (null != responseSendVoice && responseSendVoice.getResult().equals("0")) {
	    object.put("result", "0");
	    object.put("describe", "语音留言存储成功");
	    object.put("code", "0000");
	 

	    JSONObject voice_ids = SendVoiceUtil.saveNotices(called, AbilityTypeEnum.AUDEOSEND.getKey(), appExtendBean.getApp_key(), caller, chargeNbr, key,
		    accountBean.getCust_account(), appExtendBean.getPeriphery_code(), userIdentify, schedule_send_time, "", content, paramMap.get("Name"), push_url, tts_content,
		    displayNumber, batch_number, "-1", responseSendVoice.getFileName(), paramMap.get("Name"), coreDao, calledJsonArray);

	    // 拼入返回的会议id
	    object.put("voice_id", voice_ids.toString());
	} else {
	    object.put("result", "-1");
	    object.put("describe", "创建语音留言失败");
	    object.put("code", "0022");

	}

	return object;
    }

    /**
     * 荣联语音留言能力后台逻辑处理（旧版）
     * 
     * @param chargeNbr
     *            IMS发起账号
     * @param key
     *            IMS发起密码
     * @param displayNumber
     *            显号
     * @param appExtendBean
     *            应用信息
     * @param accountBean
     *            账号信息
     * @param userIdentify
     *            用户标识
     * @param caller
     *            主叫号码
     * @param called
     *            被叫号码
     * @param content
     *            短信内容
     * @param schedule_send_time
     *            预定短信发起时间
     * @param paramMap
     * @param completeFileName
     *            文件物理地址
     * @param push_url
     *            sessionid推送地址
     * @param tts_content
     *            前导音
     * @param pool_ability_id
     *            ISV自定义发起留言使用号码
     * @param batch_number
     *            是否一批次
     * @deprecated 不在维护
     * @return
     */
    private JSONObject sendAudeoNoticeRongLian(String chargeNbr, String key, String displayNumber, AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify,
	    String caller, String called, String content, String schedule_send_time, Map<String, String> paramMap, String completeFileName, String push_url, String tts_content,
	    String pool_ability_id, String batch_number, JSONArray calledJsonArray, String voice_type) {
	// 最终返回值
	JSONObject object = new JSONObject();
	// 语音留言存储状态
	ResponseSendVoiceBean responseSendVoice = null;
	try {

	    responseSendVoice = RongLianUtils.UploadFile(paramMap.get("Name"), completeFileName);
	} catch (Exception e) {
	    object.put("result", "-1");
	    object.put("describe", "创建语音留言失败" + e);
	    object.put("code", "9999");
	    e.printStackTrace();
	}
	if (null != responseSendVoice && responseSendVoice.getResult().equals("0")) {
	    object.put("result", "0");
	    object.put("describe", "语音留言存储成功");
	    object.put("code", "0000");
	    int s = uploadfileDao.insertFileInfo(coreDao, appExtendBean.getApp_key(), accountBean.getCust_account(), completeFileName, paramMap.get("Name"), paramMap.get("Name")
		    + ".wav", "","");
	    // String abilityType = AbilityTypeEnum.RONGLIANAUDEO.getKey();
	    // abilityType = VoiceUtil.changeAbilityTypeByVoiceType(abilityType,
	    // voice_type, "audio");

	    JSONObject voiceAndSessionIds = SendVoiceUtil.saveNotices(called, voice_type, appExtendBean.getApp_key(), caller, chargeNbr, key, accountBean.getCust_account(),
		    appExtendBean.getPeriphery_code(), userIdentify, schedule_send_time, "", "", paramMap.get("Name") + ".wav", push_url, tts_content, displayNumber, batch_number,
		    "-1", responseSendVoice.getFileName(), paramMap.get("Name"), coreDao, calledJsonArray);
	    if (s > -1) {
		object.put("mediaName", paramMap.get("Name"));
	    }

	    /**
	     * qinfx 2017-05-22 新增返回session_id
	     */
	    JSONObject voice_ids = voiceAndSessionIds.getJSONObject("voiceIds");
	    JSONObject session_ids = voiceAndSessionIds.getJSONObject("sessionIds");

	    // 拼入返回的会议id
	    object.put("voice_id", voice_ids.toString());
	    object.put("session_id", session_ids.toString());
	} else {
	    object.put("result", "-1");
	    object.put("describe", "创建语音留言失败," + responseSendVoice.getResult());
	    object.put("code", "0022");
	}

	return object;
    }

    /**
     * 天亿语音留言能力后台逻辑处理（旧版）
     * 
     * @param chargeNbr
     *            IMS发起账号
     * @param key
     *            IMS发起密码
     * @param displayNumber
     *            显号
     * @param appExtendBean
     *            应用信息
     * @param accountBean
     *            账号信息
     * @param userIdentify
     *            用户标识
     * @param caller
     *            主叫号码
     * @param called
     *            被叫号码
     * @param content
     *            短信内容
     * @param schedule_send_time
     *            预定短信发起时间
     * @param paramMap
     * @param completeFileName
     *            文件物理地址
     * @param push_url
     *            sessionid推送地址
     * @param tts_content
     *            前导音
     * @param pool_ability_id
     *            ISV自定义发起留言使用号码
     * @param batch_number
     *            是否一批次
     * @param voiceType
     *            处理70字、140字语音通知套餐
     * @deprecated 不在使用
     * @return
     */
    private JSONObject sendAudeoNoticeTianYi(String chargeNbr, String key, String displayNumber, AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify,
	    String caller, String called, String content, String schedule_send_time, Map<String, String> paramMap, String completeFileName, String push_url, String tts_content,
	    String pool_ability_id, String batch_number, JSONArray calledJsonArray, String voiceType) {
	// 最终返回值
	JSONObject object = new JSONObject();
	// 语音留言存储状态
	ResponseSendVoiceBean responseSendVoice = null;
	try {
	    /**
	     * 是否为青岛项目请求 70字和140字套餐为青岛项目产品
	     */
	    boolean isQingDao = false;
	    if (!EnumUtility.isBlank(voiceType) && (voiceType.contains("70") || voiceType.contains("140"))) {
		isQingDao = true;
	    }
	    responseSendVoice = TianYiUtil.UploadFile(paramMap.get("Name"), completeFileName, isQingDao);
	} catch (Exception e) {
	    object.put("result", "-1");
	    object.put("describe", "创建语音留言失败" + e);
	    object.put("code", "9999");
	    e.printStackTrace();
	}
	if (null != responseSendVoice && responseSendVoice.getResult().equals("0")) {
	    object.put("result", "0");
	    object.put("describe", "语音留言存储成功");
	    object.put("code", "0000");

	    int s = uploadfileDao.insertFileInfo(coreDao, appExtendBean.getApp_key(), accountBean.getCust_account(), completeFileName, paramMap.get("Name"), paramMap.get("Name"),
		    "","");

	    String abilityType = AbilityTypeEnum.TIANYIAUDEO.getKey();
	    abilityType = VoiceUtil.changeAbilityTypeByVoiceType(abilityType, voiceType, "audio");

	    JSONObject voiceAndSessionIds = SendVoiceUtil.saveNotices(called, abilityType, appExtendBean.getApp_key(), caller, chargeNbr, key, accountBean.getCust_account(),
		    appExtendBean.getPeriphery_code(), userIdentify, schedule_send_time, "", "", paramMap.get("Name"), push_url, tts_content, displayNumber, batch_number, "-1",
		    responseSendVoice.getFileName(), paramMap.get("Name"), coreDao, calledJsonArray);
	    if (s > -1) {
		object.put("mediaName", paramMap.get("Name"));
	    }

	    /**
	     * qinfx 2017-05-22 新增返回session_id
	     */
	    JSONObject voice_ids = voiceAndSessionIds.getJSONObject("voiceIds");
	    JSONObject session_ids = voiceAndSessionIds.getJSONObject("sessionIds");

	    // 拼入返回的会议id
	    object.put("voice_id", voice_ids.toString());
	    object.put("session_id", session_ids.toString());
	} else {
	    object.put("result", "-1");
	    object.put("describe", "创建语音留言失败," + responseSendVoice.getResult());
	    object.put("code", "0022");
	}

	return object;
    }

}
